import datetime
import random
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from conf.tools import *
from conf.set_path import path
from LOG.log import logger

'''系统里的元素'''

# 登录页面
m_ele = By.XPATH, '//input[@placeholder="请输入手机号"]'


# 获客
class HK:

    def __init__(self):
        pass

    def login(self, driver, url, iphone, pwd):
        """

        :param driver: 浏览器驱动
        :param iphone: 手机号
        :param pwd: 密码
        :return:
        """
        driver.get(url)
        driver.maximize_window()
        get_element(driver, m_ele).send_keys(iphone)
        # driver.find_element_by_xpath('//input[@placeholder="请输入手机号"]').send_keys(iphone)
        driver.find_element_by_xpath('//input[@placeholder="请输入密码"]').send_keys(pwd)
        driver.find_element_by_xpath('//div[text()="登录"]').click()
        WebDriverWait(driver, 10, 1).until(EC.visibility_of_element_located((By.XPATH, '//span[@class="username"]')))
        driver.find_element_by_id('YS-gdzj').click()
        logger.info('登陆成功')
        return driver


    def crete_order(self, driver, i, Technician):
        """

        :param driver: 浏览器驱动
        :param i: 患者名的变量
        :param Technician: 技师
        :return:
        """
        driver.find_element_by_xpath('//div[text()="固定正畸"]').click()
        logger.info('进入固定正畸页面')
        time.sleep(1)
        driver.find_element_by_xpath('//span[text()="新建订单"]').click()
        logger.info('进入新建订单页面')
        name = f'获客工具测试{i}'
        driver.find_element_by_xpath('//input[@placeholder="患者姓名"]').send_keys(name)
        logger.info(f'输入获客工具测试{i}')
        sex = random.choice(['男','女'])
        sex_loc = f'//span[text()="{sex} "]'
        driver.find_element_by_xpath(sex_loc).click()
        logger.info(f'输入性别为{sex}')
        year = random.randint(1970, 2000)
        driver.find_element_by_xpath('//div[@data-tag="年"]/input').send_keys(year)
        month = random.randint(10, 12)
        driver.find_element_by_xpath('//div[@data-tag="月"]/input').send_keys(month)
        day = random.randint(10, 28)
        driver.find_element_by_xpath('//div[@data-tag="日"]/input').send_keys(day)
        logger.info(f'输入日期为{year}-{month}-{day}')
        time.sleep(1)
        try:
            driver.find_element_by_xpath('//div[@class="foot_btn1 text-right pr-10"]//span[text()="下一步"]').click()
            logger.info(f'进入选择加工厂页面')
            time.sleep(8)
            ele = By.XPATH, '//input[@placeholder="请搜索并选择加工厂"]'
            get_element(driver, ele).click()
            time.sleep(1)
            driver.find_element_by_xpath('//span[text()="正丽科技"]').click()
            driver.find_element_by_xpath('//*[@id="ZJaddWin"]/div/div[3]/div[2]/div[2]/div[1]/div/div').click()  # 快速下单
            logger.info(f'选择快速下单')
            time.sleep(1)
            driver.find_element_by_id('hgx-input').send_keys('测试')
            time.sleep(1)
            driver.find_element_by_xpath('//span[text()="下一步"]').click()
            time.sleep(1)
            logger.info('进入上传模型页面')
            driver.find_element_by_xpath(
                '//div[@class="upimgbg imgs1"]/following-sibling::div[1]/button[@class="yg-button default"]').click()
            time.sleep(1)
            upload(r'C:\Users\FEISEN\Desktop\项目\获客工具\自动排牙\上下颌\s0-2020-T.stl')
            logger.info('开始上传上颌数据')
            up_ele_stl = By.XPATH, '//*[@id="ZJaddWin"]/div/div[3]/div[2]/div[1]/div[2][@style="display: none;"]'
            get_element(driver, up_ele_stl)
            logger.info('上颌数据上传完成')
            # time.sleep(10)
            driver.find_element_by_xpath(
                '//div[@class="upimgbg imgs2"]/following-sibling::div[1]/button[@class="yg-button default"]').click()
            driver.find_element_by_xpath('//div/button[@class="yg-button default"]')
            logger.info('开始上传下颌数据')
            upload(r'C:\Users\FEISEN\Desktop\项目\获客工具\自动排牙\上下颌\x0-2020-T.stl')
            low_ele_stl = By.XPATH, '//*[@id="ZJaddWin"]/div/div[3]/div[2]/div[2]/div[2][@style="display: none;"]'
            get_element(driver, low_ele_stl)
            logger.info('下颌数据上传完成')
            driver.find_element_by_xpath('//div[@class="foot_btn1 text-right pr-10"]//span[text()="下一步"]').click()
            time.sleep(2)
            logger.info('进入好友列表页面')
            driver.find_element_by_xpath('//span[text()="好友列表"]').click()
            time.sleep(1)
            driver.find_element_by_xpath(f'//span[text()="{Technician}"]').click()
            logger.info(f'选择{Technician}')
            time.sleep(1)
            driver.find_element_by_xpath('//span[text()="提交"]').click()
            logger.info('提交订单')
            time.sleep(8)
            driver.find_element_by_class_name('yg-input__inner').send_keys(name)
            logger.info('进入首页，输入患者姓名')
            time.sleep(1)
            driver.find_element_by_xpath('//span[text()="查询"]').click()
            logger.info('点击查询')
            time.sleep(1)
            driver.find_element_by_xpath('//i[text()="详情"]').click()
            logger.info('进入详情页面')
            time.sleep(1)
            driver.find_element_by_xpath('//span[text()="查看"]').click()
            logger.info('查看数据')
            time.sleep(2)
        except Exception as e:
            name = f"error{str(datetime.datetime.now().strftime('%Y%m%d%H%M%S'))}.png"
            driver.save_screenshot(rf"{path.image_dir}\{name}")
            logger.exception(e)



    def huoke(self, driver):
        try:
            ele_1 = driver.find_element_by_xpath('//*[@id="app"]/div[3]/div/div[1]/div/span[text()="0%"]')
            get_element(driver, ele_1)
            time.sleep(1)
            driver.find_element_by_xpath('//span[text()="+自动排牙"]').click()
            logger.info('进入自动排牙页面')
            time.sleep(1)
            ele_2 = driver.find_element_by_xpath('//*[@id="app"]/div[3]/div/div[1]/div/span[text()="0%"]')
            get_element(driver, ele_2)
            time.sleep(1)
            driver.find_element_by_xpath('//span[text()="下一步"]').click()  # 手动正定页面
            logger.info('进入正定页面')
            self.ele(driver)

            driver.find_element_by_xpath('//span[text()="下一步"]').click()  # 上颌边缘线页面
            logger.info('进入上颌边缘线页面')
            self.ele(driver)
            driver.find_element_by_xpath('//span[text()="下一步"]').click()  # 上颌牙齿编号
            logger.info('进入上颌牙齿编号页面')
            self.ele(driver)
            driver.find_element_by_xpath('//span[text()="下一步"]').click()  # 下颌边缘线页面
            logger.info('进入下颌边缘线页面')
            self.ele(driver)
            ele = driver.find_element_by_xpath('//span[text()="下一步"]')  # 下颌牙齿编号
            webdriver.ActionChains(driver).move_to_element(ele).click(ele).perform()
            logger.info('进入下颌牙齿编号页面')
            self.ele(driver)
            driver.find_element_by_xpath('//span[text()="下一步"]').click()  # 上颌坐标系
            logger.info('进入上颌坐标系页面')
            self.ele(driver)
            driver.find_element_by_xpath('//span[text()="下一步"]').click()  # 下颌坐标系
            logger.info('进入下颌坐标系页面')
            logger.info('进入排牙设置窗口')
            # time.sleep(1)
            driver.find_element_by_xpath('//div[@style="text-align: right;"]/button[text()="确定"]').click()
            self.ele(driver)
            logger.info('进入排牙方案')
            time.sleep(1)
            driver.find_element_by_xpath('//*[@id="app"]/div[8]/div//i[@class="yg-icon-close"]').click()  # 关闭排牙方案
            logger.info('关闭排牙方案')
            time.sleep(2)
        except Exception as e:
            name = f"error{str(datetime.datetime.now().strftime('%Y%m%d%H%M%S'))}.png"
            driver.save_screenshot(rf"{path.image_dir}\{name}")
            logger.exception(e)

    def ele(self, driver):
        """
        自定义显示等待元素的方法
        :param driver: 浏览器驱动
        :return:
        """
        time.sleep(2)
        ele = driver.find_element_by_xpath('//*[@id="app"]/div[3]/div/div[1]/div/span[text()="0%"]')
        get_element(driver, ele)

hk = HK()
# if __name__ == '__main__':
#
#     driver = webdriver.Chrome()
#     driver.implicitly_wait(100)
#     driver.maximize_window()
#     excel.write_excel(l=1, h=row, val=f"{str(datetime.now().strftime('%Y-%m-%d %H:%M:%S'))}")  # 记录开始执行时间
#     hk = HK()
#     # hk.login(driver, '19900000002', '111111')
#     hk.login(driver, 'https://dx.yayi360.com/#/', '13698420336', '123456')
#     i = 86  # 患者命名
#     count = 0  # 执行次数
#     while True:
#         i += 1
#
#         # hk.crete_order(driver, i, '正丽技师一号')
#         hk.crete_order(driver, i, '小丸子')
#         hk.huoke()

